package com.zs.zcerp.approval.controller;

import com.zs.zcerp.approval.entity.QhRejectRecordEntity;
import com.zs.zcerp.approval.service.IQhChkmgrdService;
import com.zs.zcerp.approval.service.IQhChkmgrmService;
import com.zs.zcerp.approval.service.QhRejectRecordService;
import com.zs.zcerp.common.annotation.ControllerEndpoint;
import com.zs.zcerp.common.controller.BaseController;
import com.zs.zcerp.common.entity.FebsResponse;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.entity.Strings;
import com.zs.zcerp.common.exception.FebsException;
import com.zs.zcerp.common.utils.ConstantUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * (驳回签核)表控制层
 *
 * @author henry
 * @since 2021-06-16 15:01:39
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("reject")
public class QhRejectRecordController  extends BaseController {
    /**
     * 服务对象
     */
    private final  QhRejectRecordService qhRejectRecordService;

    /***
     * 签核主表Service
     */
    private final IQhChkmgrmService iQhChkmgrmService;
    /***
     * 签核子表Service
     */
    private final IQhChkmgrdService iQhChkmgrdService;

    /**
     * 获取多条驳回
     * @param rejectRecordEntity
     * @return
     */
    @GetMapping("getList")
    @RequiresPermissions("reject:view")
    public FebsResponse baseList(@Valid QhRejectRecordEntity rejectRecordEntity) {
        return new FebsResponse().success().data(qhRejectRecordService.findList(rejectRecordEntity));

    }


    @GetMapping("delete/{basestdNos}")
    @RequiresPermissions("reject:delete")
    @ControllerEndpoint(operation = "删除驳回", exceptionMessage = "删除驳回失败")
    public FebsResponse delete(@NotBlank(message = "{required}") @PathVariable String basestdNos) {
        qhRejectRecordService.deleteBaseByRejectId(StringUtils.split(basestdNos, Strings.COMMA));
        return new FebsResponse().success();
    }

    /**
     * 增加驳回
     * @param rejectRecordEntity
     * @return
     */
    @PostMapping("add")
    @RequiresPermissions("reject:add")
    @ControllerEndpoint(operation = "新增驳回", exceptionMessage = "新增驳回失败")
    @ResponseBody
    public FebsResponse addBase(@RequestBody  QhRejectRecordEntity rejectRecordEntity) {
        if (rejectRecordEntity == null) {
            throw new FebsException("新增驳回为空");
        }
        qhRejectRecordService.createEntity(rejectRecordEntity);
        /**
         * 删除erp签核数据
         */
       iQhChkmgrmService.deleteByOrderyNo(rejectRecordEntity.getOrderNo());
       iQhChkmgrdService.deleteByOrderyNo(rejectRecordEntity.getOrderNo());

        /**
         * 回签erp数据 变成驳回状态 销毁
         */
        if(ConstantUtil.DESTORY.equals(rejectRecordEntity.getClassify())) {
            iQhChkmgrdService.rejectDestoryStatus(rejectRecordEntity.getOrderNo());
        }
        /**回签erp数据 变驳回状态 样品*/
        if(ConstantUtil.DESTORY_Y.equals(rejectRecordEntity.getClassify())) {
            iQhChkmgrmService.rejectYqStatus(rejectRecordEntity.getOrderNo());
        }

        /** type19 补料采购单   补料总表转采购单 在pomOrder  */

        /** type21 加工变更单*/
        if(ConstantUtil.inContains(ConstantUtil.WPGOrder,(rejectRecordEntity.getClassify()))){
            /**2019.11.04 变更单无论通过或是退回都变为生效状态 联越ERP原备注*/
            iQhChkmgrmService.WPGignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderEffective);
        }
        //type22 固定资产异动单号
        if(ConstantUtil.inContains(ConstantUtil.FATRorder,(rejectRecordEntity.getClassify()))){
            iQhChkmgrmService.FATRSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }
        if(ConstantUtil.inContains(ConstantUtil.FAINorder,(rejectRecordEntity.getClassify()))){
            iQhChkmgrmService.FAINSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }
        if(ConstantUtil.inContains(ConstantUtil.XPOOrder,(rejectRecordEntity.getClassify()))){
            iQhChkmgrmService.XPOSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }
        /** type26 采购变更单  XPC采购单变更驳回不修改状态*/
        if(ConstantUtil.inContains(ConstantUtil.XPCOrder,(rejectRecordEntity.getClassify()))){
            iQhChkmgrmService.XPCSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderSigning);
        }
        if (ConstantUtil.inContains(ConstantUtil.GAPOOrder, rejectRecordEntity.getClassify())) {
            iQhChkmgrmService.GAPOSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }
        if (ConstantUtil.inContains(ConstantUtil.MDPOOrder, rejectRecordEntity.getClassify())) {
            iQhChkmgrmService.MDPOSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }
        if (ConstantUtil.inContains(ConstantUtil.pomOrder, rejectRecordEntity.getClassify())) {
            iQhChkmgrmService.PO6SignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }
        if (ConstantUtil.inContains(ConstantUtil.PGOrder, rejectRecordEntity.getClassify())) {
            iQhChkmgrmService.PGSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }
        if (ConstantUtil.inContains(ConstantUtil.kfOrder, rejectRecordEntity.getClassify())) {
            iQhChkmgrmService.KFPSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }
        if (ConstantUtil.inContains(ConstantUtil.woOrder, rejectRecordEntity.getClassify())) {
            iQhChkmgrmService.WOSignStatus(rejectRecordEntity.getOrderNo(), ConstantUtil.orderCancel);
        }

        return new FebsResponse().success();
    }

    @GetMapping("list")
    @RequiresPermissions("reject:view")
    public FebsResponse signList(QhRejectRecordEntity rejectRecordEntity, QueryRequest request) {
        if(rejectRecordEntity.getOrderNo()!=null){
            rejectRecordEntity.setOrderNo(rejectRecordEntity.getOrderNo().trim());
        }
        if(rejectRecordEntity.getSendUserCname()!=null){
            rejectRecordEntity.setSendUserCname(rejectRecordEntity.getSendUserCname().trim());
        }
        return new FebsResponse().success()
                .data(getDataTable(qhRejectRecordService.findPageList(rejectRecordEntity, request)));
    }

}